/**
 * ESUI (Enterprise Simple UI)
 * Copyright 2013 Baidu Inc. All rights reserved.
 *
 * @ignore
 * @file RadioTab
 * @author hongfeng@baidu.com
 */

define(
    function (require) {

        var esui = require('esui');
        var u = require('underscore');
        var Extension = require('esui/Extension');
        var eoo = require('eoo');
        var $ = require('jquery');

        var RadioTab = eoo.create(
            Extension,
            {
                /**
                 * 指定扩展类型，始终为`"RadioTab"`
                 *
                 * @type {string}
                 */
                type: 'RadioTab',

                attachTo: function () {
                    this.$super(arguments);

                    var me = this;
                    if (this.isActive()) {
                        setTimeout(function () {
                            appendPanel.call(me);
                            togglePanel.call(me);
                        }, 0);
                    }

                    this.target.on('change', function () {
                        if (me.isActive()) {
                            togglePanel.call(me);
                        }
                    });
                },

                /**
                 * 激活扩展
                 *
                 * @override
                 */
                activate: function () {
                    // 只对`BoxGroup`控件生效
                    var type = this.target.type;

                    if (type !== 'BoxGroup') {
                        return;
                    }
                    this.$super(arguments);
                },

                /**
                 * 取消扩展的激活状态
                 *
                 * @override
                 */
                inactivate: function () {
                    // 只对`BoxGroup`控件生效
                    var type = this.target.type;

                    if (type !== 'BoxGroup') {
                        return;
                    }
                    this.$super(arguments);
                }
            }
        );

        /**
         * 初始化一些class，如果是纵向则改变Panel位置
         */
        function appendPanel() {
            var target = this.target;
            var datasource = target.datasource;
            if (!datasource.length) {
                return;
            }

            var jqMain = $(target.main);
            var helper = target.helper;
            jqMain.addClass(helper.getPrimaryClassName('arrow'));

            var isVertical = target.orientation === 'vertical';
            u.each(
                datasource,
                function (item, index) {
                    var id = getPanelId.call(this, item, index);
                    var jqPanel;
                    if (id) {
                        jqPanel = jqMain.parent().find('#' + id);
                        jqPanel.attr('data-radio-panel-id', helper.getId(id));
                    }

                    var jqRadio = $(target.getBoxElements()[index]).parent();
                    if (jqRadio.length) {
                        if (isVertical) {
                            jqRadio.after(jqPanel);
                        }
                        else {
                            // 如果radio选项对应存在panel，就在panel上加上横线等样式
                            // 否则在radio上隐藏三角
                            if (jqPanel && jqPanel.length) {
                                jqPanel.addClass(helper.getPrefixClass('panel-boxgroup-border'));
                            }
                            else {
                                jqRadio.addClass(helper.getPrimaryClassName('arrow-hidden'));
                            }
                        }
                    }
                },
                this
            );
        }

        // 记录是否要记录panel内控件disabled状态的标志位
        var DISABLE_DONE_FLAG = 'isDisableDone';

        /**
         * 控制Panel显示隐藏
         */
        function togglePanel() {
            var target = this.target;
            var value = target.getValue();
            var datasource = target.datasource;
            var helper = target.helper;
            u.each(
                datasource,
                function (item, index) {
                    var id = getPanelId.call(this, item, index);
                    if (!id) {
                        return;
                    }
                    id = helper.getId(id);
                    var panel = $('[data-radio-panel-id="' + id + '"]');
                    var elements = panel.find('[data-ctrl-id]');
                    var show = value === item.value;
                    // 保证初始化时未选中项能正常隐藏，这里放在if上面
                    panel.toggle(show);

                    // 记录disable在一次显示中只能做一次，否则就会记录错误状态
                    // 只有panle的状态发送改变后，才需要再次记录
                    if (!panel.data(DISABLE_DONE_FLAG)) {
                        u.each(
                            elements,
                            function (element) {
                                var control = esui.getControlByDOM(element);
                                element = $(element);
                                element.data('isDisabled', control.isDisabled());
                                control.setDisabled(true);
                            }
                        );
                        panel.data(DISABLE_DONE_FLAG, true);
                    }

                    if (show) {
                        panel.data(DISABLE_DONE_FLAG, false);
                        u.each(
                            elements,
                            function (element) {
                                var control = esui.getControlByDOM(element);
                                element = $(element);
                                if (!element.data('isDisabled')) {
                                    control.setDisabled(false);
                                }
                            }
                        );
                    }
                },
                this
            );
        }

        function getPanelId(item, index) {
            var target = this.target;
            var id = '';
            if (item.panel || item.id) {
                id = item.panel || (item.id + '-panel');
            }
            else if (target.name) {
                id = target.name.replace(/\./g, '-') + '-panel-' + index;
            }
            return id;
        }

        esui.registerExtension(RadioTab);
        return RadioTab;
    }
);
